(function(window, $) {
	var _targetId = null;
	var _interval = null;
	function generatePlayer(musicUrl) {
		$('body').append("<div id='_audioPlayer' style='width:1px; height:1px;'></div>");
		var audioPlayer = QT_GenerateOBJECTText(musicUrl, '1', '1', '', 'AUTOPLAY', 'false', 'emb#NAME', "_audioPlayer", 'obj#id', "_audioPlayer", 'enablejavascript', 'true', 'ControllerVisible', 'true', 'postdomevents', 'true', 'emb#id', 'qtmovie_embed');
		$("#_audioPlayer").append($(audioPlayer));
	}

	function audioPlayerEnded() {
		$("#" + _targetId).html('<font style=\'text-decoration:underline; color: green;\'>播放</font>');
		_targetId = null;
		window.clearInterval(_interval);
	}

	function amrAudioPlay(musicUrl, id) {
		if (!document._audioPlayer) {
			generatePlayer(musicUrl);
		}
		if (_targetId !== null) {
			//当前正在播放
			$("#" + _targetId).html('<font style=\'text-decoration:underline; color: green;\'>播放</font>');
			if (_targetId === id) {
				//当前是自己在播放
				document._audioPlayer.Stop();
				_targetId = null;
				window.clearInterval(_interval);
				return;
			}
		}
		document._audioPlayer.SetURL(musicUrl);
		_targetId = id;
		$("#" + _targetId).html('<font style=\'text-decoration:underline; color: red;\'>停止</font>');
		window.clearInterval(_interval);
		_interval = window.setInterval(function() {
			var time = document._audioPlayer.GetTime();
			var totalTime = document._audioPlayer.GetEndTime();
			//console.log(time + '  ' + totalTime);
			if (time === totalTime) {
				audioPlayerEnded();
			}
		}, 300);
	}

	window.amrAudioPlay = amrAudioPlay;
})(window, jQuery);
